package com.huaru.formboot.form.service.impl;


import com.huaru.formboot.form.dao.TableTemplateMapper;
import com.huaru.formboot.form.entity.Dto.TableTemplate;
import com.huaru.formboot.form.entity.vo.TableTemplateVo;
import com.huaru.formboot.form.service.TableTemplateService;
import com.huaru.formboot.form.util.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 表单模板表 服务实现类
 * </p>
 *
 * @author wz
 * @since 2021-04-15
 */
@Service
public class TableTemplateServiceImpl  implements TableTemplateService {
    @Autowired
    private TableTemplateMapper tableTemplateMapper;
    @Override
    public List<TableTemplateVo> getAllTableTemplate() {

        List<TableTemplateVo> tree = new ArrayList<>();
        try {
            List<TableTemplateVo> allTableTemplate =tableTemplateMapper.getAllTableTemplate();
                    Tree<String> stringTree = new Tree<>();
            String pid = "-1";
            tree = stringTree.createTree(allTableTemplate, pid);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return tree;
    }

    @Override
    public Integer insertRootNode(TableTemplate tableTemplate) {
        String tableParentId = "-1";
        TableTemplate tableTemp = new TableTemplate(
                UUID.randomUUID().toString(),
                tableTemplate.getTableName(),
                tableParentId,
                tableTemplate.getTableSort(),
                LocalDateTime.now(),
                LocalDateTime.now());
        return tableTemplateMapper.insertNode(tableTemp);
    }

    @Override
    public Integer insertLowerNode(TableTemplate tableTemplate) {
        tableTemplate.setTableId(UUID.randomUUID().toString());
        tableTemplate.setCreateTime(LocalDateTime.now());
        tableTemplate.setUpdateTime(LocalDateTime.now());
        return tableTemplateMapper.insertNode(tableTemplate);
    }

    @Override
    public Integer updateNode(TableTemplate tableTemplate) {
         tableTemplate.setUpdateTime(LocalDateTime.now());
        return tableTemplateMapper.updateNode(tableTemplate);
    }

    @Override
    public Integer deletedNode(String tableId) {
        return tableTemplateMapper.deletedNode(tableId);
    }
}
